package xhwl.insurance.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import de.greenrobot.event.EventBus;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.customView.ProgressWebView;
import xhwl.insurance.event.SubmitInfoEvent;
import xhwl.insurance.http.api.SaleInfoApi;
import xhwl.insurance.http.bean.resp.SaleInfoResp;
import xhwl.insurance.util.AppUtil;
import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

public class SaleCardDetailActivity extends BaseActivity implements View.OnClickListener {

    private ProgressWebView webview;
    private String URL;
    String fenxiang = "";
    String cardpic = "";
    String cardprice = "";
    String saletype = "";
    String yw_id = "";
    String card_name = "";
    String title="";
    private Button buy;
    public static SaleCardDetailActivity instance;
    String c_type;
    RelativeLayout rlContent;
    LinearLayout llNetUnavalible;
    TextView tvRefresh;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        String jumpurl = getIntent().getStringExtra("jumpurl");

        if (null != getIntent().getStringExtra("cardpic"))
            cardpic = getIntent().getStringExtra("cardpic");
        if (null != getIntent().getStringExtra("cardprice"))
            cardprice = getIntent().getStringExtra("cardprice");//价格
        if (null != getIntent().getStringExtra("yw_id"))
            yw_id = getIntent().getStringExtra("yw_id");
        if (null != getIntent().getStringExtra("saletype"))
            saletype = getIntent().getStringExtra("saletype");
        if (null != getIntent().getStringExtra("card_name"))
            card_name = getIntent().getStringExtra("card_name");
        if (null != getIntent().getStringExtra("c_type"))
            c_type = getIntent().getStringExtra("c_type");
        if (null != jumpurl)
            URL = jumpurl + "&footer=hide";
        setContentView(R.layout.activity_sale_card_detail);
        instance = this;
    }

    @Override
    public void initViews() {
        initTitleViews();
        rlContent= (RelativeLayout) findViewById(R.id.rlContent);
        llNetUnavalible= (LinearLayout) findViewById(R.id.llNetUnavalible);
        tvRefresh= (TextView) findViewById(R.id.tvRefresh);

        webview = (ProgressWebView) findViewById(R.id.webview);
        buy = (Button) findViewById(R.id.buy);
        webview.getSettings().setDefaultTextEncodingName("UTF-8");
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
        WebChromeClient wvcc = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                titleTextView.setText(title);
            }

        };
        // 设置setWebChromeClient对象
        webview.setWebChromeClient(wvcc);
    }

    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        titleTextView.setText(title);
        rightTextView.setVisibility(View.GONE);
    }

    @Override
    public void initListener() {
        buy.setOnClickListener(this);
        webview.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });

        llBack.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (canGoBack()) {
                    goBack();
                } else {
                    SaleCardDetailActivity.this.finish();
                }
            }
        });
    }

    @Override
    public void loadData() {
        if(!AppUtil.iskNetAvailable(mContext)){
            rlContent.setVisibility(View.GONE);
            llNetUnavalible.setVisibility(View.VISIBLE);
            titleTextView.setText(card_name);
            tvRefresh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    loadData();
                }
            });
            return;
        }
        else
        {
            rlContent.setVisibility(View.VISIBLE);
            llNetUnavalible.setVisibility(View.GONE);
        }
        SaleInfoApi api = new SaleInfoApi();
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<SaleInfoResp>() {
            @Override
            public void onNext(SaleInfoResp resp) {

                String user_id = "";
                if (null != resp.user_id)
                    user_id = resp.user_id;
                String user_name = "";
                if (null != resp.user_name)
                    user_name = Base64.encodeToString(resp.user_name.getBytes(), Base64.NO_WRAP);
                String link_mobile = "";
                if (null != resp.link_mobile)
                    link_mobile = resp.link_mobile;
                fenxiang = "&user_id=" + user_id + "&link_mobile=" + link_mobile + "&user_name=" + user_name;
            }

            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
            }
        });
        webview.loadUrl(URL);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack(); // goBack()表示返回WebView的上一页面
            return true;
        }
        SaleCardDetailActivity.this.finish();
        return false;

    }

    public boolean canGoBack() {
        return webview != null && webview.canGoBack();
    }

    public void goBack() {
        if (webview != null) {
            webview.goBack();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.buy://添加被保障人
//                Intent intentbuy = new Intent(this, AddPatientVisitsActivity.class);
                Intent intentbuy = new Intent(this, SubmitSaleInfoActivity.class);
                intentbuy.putExtra("cardprice", null != cardprice ? cardprice : "");
                intentbuy.putExtra("saletype", null != saletype ? saletype : "");
                intentbuy.putExtra("yw_id", null != yw_id ? yw_id : "");
                intentbuy.putExtra("card_name", card_name);
                intentbuy.putExtra("c_type", c_type);
                startActivity(intentbuy);
                break;
        }
    }
    public void onEvent(SubmitInfoEvent event){
        if(event!=null&&event.isComplete()){
            finish();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
